package cm.mefobe.android.telecomcost.view;

import cm.mefobe.android.telecomcost.Utility;
import android.content.Context;
import android.graphics.Canvas;
import android.text.InputType;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class AmountView extends TableLayout {

	private Context _context;
	private TextView _label;
	private EditText _txtAmount;
	private TableRow row;

	public double getAmount() {
		return Double.parseDouble(_txtAmount.getText().toString().trim());
	}

	public void setAmount(double amount) {
		if (_txtAmount == null) {
			setAmountView();
		}

		_txtAmount.setText(Utility.padRight(Double.toString(amount), 7));
	}

	public AmountView(Context context, String label) {
		super(context);
		Log.i("AmountView", "creating the time picker view for " + label);
		_context = context;
		_label.setText(label);
	}

	private void setAmountView() {
		if (isInEditMode()) {
			Log.i("AmountView", "isInEditMode");
		} else {
			if (row == null) {
				Log.i("AmountView", "adding views...");
				row = new TableRow(_context);
				_label = new TextView(_context);
				_txtAmount = new EditText(_context);

				_txtAmount.setInputType(InputType.TYPE_CLASS_NUMBER);
				_label.setText("Amount : ");

				row.addView(_label);
				row.addView(_txtAmount);				

				addView(row);
				setColumnStretchable(0, true);
				setAmount(0);
			} else {
				Log.i("AmountView", "AmountView already set");
			}
		}
	}

	public AmountView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.i("AmountView", "creating the time picker view with AttributeSet");

		_context = context;
		setAmountView();
	}

	public AmountView(Context context) {
		super(context);
		Log.i("AmountView", "creating the time picker view");
		_context = context;
		setAmountView();
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		Log.i("AmountView", "onLayout");
		super.onLayout(changed, l, t, r, b);
	}

	@Override
	protected void onAttachedToWindow() {
		Log.i("AmountView", "onAttachedToWindow");
		super.onAttachedToWindow();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.i("AmountView", "onDraw");
		super.onDraw(canvas);
	}

	@Override
	protected void onFinishInflate() {
		Log.i("AmountView", "onFinishInflate");
		super.onFinishInflate();
		setAmountView();
	}

	@Override
	public void updateViewLayout(View view,
			android.view.ViewGroup.LayoutParams params) {
		Log.i("AmountView", "updateViewLayout");
		super.updateViewLayout(view, params);
	}

	public void setLabel(String label) {
		if (_label == null) {
			setAmountView();
		}

		_label.setText(label);
	}

	public EditText getEditText() {
		if(_txtAmount == null)
		{
			setAmountView();
		}
		
		return _txtAmount;
	}
}
